package com.xlh.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wz.activity.main.helper.MainInputBox;
import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.bean.DataBase.ButCmd;
import com.xlh.bean.TitleBean;
import com.xlh.fgs.BtnsFg;
import com.xlh.outside.MainActivity;
import com.xlh.outside.R;

import java.util.List;

public class UserBtnAreaAdapter extends BaseAdapter {

	private List<TitleBean> data;
	private LayoutInflater mInflater;
	private Context context;//sendActionToServer
	private List<ButCmd> butCmdList;
	public ButCmd tmpButCmd;



	public UserBtnAreaAdapter(Context context, List<TitleBean> data) {
			// TODO Auto-generated constructor stub
			mInflater = LayoutInflater.from(context);

			this.data = data;
			this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}
	public TitleBean getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}



 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder ;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.userbtn_area_item, parent, false);
			holder = new ViewHolder();

			holder.tv01 = (TextView) convertView.findViewById(R.id.userbtn_area_item);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}


		TitleBean	 titleBean 	= data.get(position);
		final String sendCmd 				= titleBean.getmAction();

		final int kid 			= titleBean.getId();

		GradientDrawable drawable = (GradientDrawable) holder.tv01.getBackground();
		//边框
		drawable.setStroke(FUti.dip2px(convertView.getContext(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
		//设置背景
		drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));


		holder.tv01.getLayoutParams().height = FUti.dip2px(context,40);

		holder.tv01.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));


		holder.tv01.setText( titleBean.getSpanned() );






		holder.tv01.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {


				((MainActivity)context).sendActionToServer(sendCmd);


			}
		});

		holder.tv01.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {

				new MainInputBox(context)
						.setParentButKey(((MainActivity)context).getActiveButKey())
						.setBuKey(String.valueOf( kid ))
						.run();
				return true;
			}
		});


		/*
		if ( titleBean.getColor() == null ){
			holder.tv01.setText( titleBean.getmText() );
		}else{
			//这是按钮的上一排文字
			holder.tv01.setText(Html.fromHtml("<font color='" + titleBean.getColor() + "'>" + titleBean.getmText() + "</font>"));

		}*/

		//holder.tv01.setText(Html.fromHtml("<font color='" + titleBean.getColor()==null?"":titleBean.getColor() + "'>" + titleBean.getmText() + "</font>"));


		return convertView;
	}

	private class ViewHolder {
		
		private TextView tv01;

		
	}
	
	public void addTitle(TitleBean bean) {
		// TODO Auto-generated method stub
		if (data != null) {
			data.add(bean);
		}
		this.notifyDataSetChanged();
	}

	public void setData(List<TitleBean> mTitleData) {
		// TODO Auto-generated method stub
		if(mTitleData != null) {
			this.data = mTitleData;
		}
		this.notifyDataSetChanged();
	}

	public void clear() {
		// TODO Auto-generated method stub
		if(data != null) {
			data.clear();
		}
		this.notifyDataSetChanged();
	}

	public void addTitle(List<TitleBean> ts) {
		// TODO Auto-generated method stub
		
		if(this.data != null) {
			
			for (int i = 0; i < ts.size(); i++) {
				this.data.add(ts.get(i));
			}
		}
		this.notifyDataSetChanged();
	}
	
}
